package com.spring.cloud.utils;

import com.spring.cloud.enums.BusinessMessage;
import com.spring.cloud.enums.SuccessMsgEnum;
import com.spring.cloud.vo.JsonResult;
import com.spring.cloud.vo.JsonResultImpl;

/**
 * 响应模型工具类
 */
public class JsonResultUtil {

    private static final Object EMPTY_DATA[] = null;

    public static<T> JsonResult<T> ok(T data) {
        JsonResult<T> result = new JsonResultImpl<>();

        BusinessMessage ok = SuccessMsgEnum.S0;

        result.setCode(ok.getCode());
        result.setMsg(ok.getMsg());
        result.setData(data);
        return result;
    }

    public static<T> JsonResult<T> ok(BusinessMessage businessMessage, T data) {
        JsonResult<T> result = new JsonResultImpl<>();
        result.setCode(businessMessage.getCode());
        result.setMsg(businessMessage.getMsg());
        result.setData(data);
        return result;
    }

    public static JsonResult ok(BusinessMessage businessMessage) {
        return emptyData(businessMessage);
    }

    public static JsonResult failure(BusinessMessage businessMessage) {
        return emptyData(businessMessage);
    }

    public static JsonResult failure(String validMsg) {
        JsonResult<Object> result = new JsonResultImpl<>();
        result.setCode(-10000);
        result.setMsg(validMsg);
        result.setData(EMPTY_DATA);
        return result;
    }

    private static JsonResult emptyData(BusinessMessage businessMessage) {
        JsonResult<Object> result = new JsonResultImpl<>();
        result.setCode(businessMessage.getCode());
        result.setMsg(businessMessage.getMsg());
        result.setData(EMPTY_DATA);
        return result;
    }



}
